package cn.coconet.clothingguide.service;

import cn.coconet.clothingguide.tool.ClothingTool;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.UserMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import dev.langchain4j.service.SystemMessage; // 新增：导入系统提示注解
import dev.langchain4j.model.chat.ChatModel;

import java.time.Duration;

interface GuideService {
    @SystemMessage("你是服装导购AI，根据用户需求调用工具查询商品并推荐")
    String chat(@UserMessage String userInput);
}

@Service
public class ClothingGuideService {

    private final GuideService guideService;

    @Autowired
    public ClothingGuideService(ClothingTool clothingTool,
                               @Value("${openai.api.key}") String openAiApiKey) {

        // 1. 构建OpenAi模型并转换为ChatLanguageModel接口
        ChatModel chatModel = OpenAiChatModel.builder()
                .apiKey(openAiApiKey)
                .modelName("gpt-3.5-turbo")
                .timeout(Duration.ofSeconds(30))
                .build();  // 关键转换步骤

        // 2. 配置对话记忆
        var memory = MessageWindowChatMemory.withMaxMessages(10);

        // 3. 构建AI服务
        this.guideService = AiServices.builder(GuideService.class)
                .chatModel(chatModel)  // 现在类型匹配
                .tools(clothingTool)
                .chatMemory(memory)
                .build();
    }

    public String getRecommendation(String userInput) {
        return guideService.chat(userInput);
    }
}